package com.ruoyi.controller;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.edit.domain.vo.EditVo;
import com.ruoyi.edit.domain.vo.FileVo;
import com.ruoyi.edit.domain.vo.OrderVo;
import com.ruoyi.edit.service.IEditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/edit")
public class EditController extends BaseController {
    @Autowired
    private IEditService editService;

    @PreAuthorize("@ss.hasPermi('edit:list')")
    @GetMapping("/list")
    public TableDataInfo list(EditVo editEntity) {
        startPage();
        List<EditVo> list = editService.selectList(editEntity);
        return getDataTable(list);
    }


    @GetMapping(value = "/{editId}")
    public AjaxResult getInfo(@PathVariable Long editId) {
        return success(editService.selectEditById(editId));
    }


    @Log(title = "编辑管理", businessType = BusinessType.UPDATE)
    @PutMapping("update")
    public AjaxResult update(@Validated @RequestBody EditVo vo) {
        return toAjax(editService.updateEdit(vo));
    }

    @Log(title = "编辑管理", businessType = BusinessType.UPDATE)
    @PutMapping("changeStatus")
    public AjaxResult changeStatus(@Validated @RequestBody EditVo vo) {
        return toAjax(editService.updateUserStatus(vo));
    }


    @GetMapping("/orderList")
    public TableDataInfo getOrderList(EditVo editEntity) {
        // startPage();
        List<OrderVo> list = editService.getOderByUserId(editEntity.getEditId());
        return getDataTable(list);
    }

    @Log(title = "编辑管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EditVo editEntity) {
        List<EditVo> list = editService.selectList(editEntity);
        ExcelUtil<EditVo> util = new ExcelUtil<EditVo>(EditVo.class);
        util.exportExcel(response, list, "编辑管理");
    }

    @GetMapping("getFileList")
    public TableDataInfo getFileList(OrderVo orderVo) {
        // startPage();
        List<FileVo> list = editService.getFileList(orderVo);
        return getDataTable(list);
    }

    @DeleteMapping("/deleteFile/{fileId}")
    public AjaxResult deleteFile(@PathVariable Long fileId) {
        return toAjax(editService.deleteFile(fileId));
    }
}


